class Report < ActiveRecord::Base
  belongs_to :promotion_mail

  after_find :reload

  # def delivered
  #   @delivered ||= @content_obj['delivered']
  # end
  %i(delivered arrived opens clicks).each do |attr|
    class_eval(<<-EOS, __FILE__, __LINE__ + 1)
      def #{attr}
        @#{attr} ||= @content_obj['#{attr}']
      end
    EOS
  end

  def arrival_rates
    @arrival_rates ||= arrived.map{|k, v| [k, v / delivered[k]]}.to_h
  end

  def open_rate
    @open_rate ||= opens['total'] / arrived['total']
  end

  def click_rate
    @click_rate ||= clicks['total'] / arrived['total']
  end

  private
  def reload
    @content_obj = MultiJson.load(content)
  end
end
